package com.xzc._3_running;

import com.xzc._1_build.Base_Test;
import org.drools.core.event.DebugRuleRuntimeEventListener;
import org.drools.core.event.DefaultAgendaEventListener;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.event.rule.AfterMatchFiredEvent;
import org.kie.api.logger.KieRuntimeLogger;
import org.kie.api.runtime.KieSession;

/**
 * 事件模型
 * 事件实现KieRuntimeEvent，这样可以在运行时回收
 */
public class EventModel {

    Base_Test t = new Base_Test();
    KieBase kBase1;
    KieSession kSession;

    public void init() {
        t.createContainerByClasspath();
        KieBase kBase1 = t.getKieContainer().getKieBase("KBase1");
        KieSession kSession = t.getKieContainer().newKieSession("KSession2_1");
    }

    /**
     * 给KieSession加入审计事件
     */
    public void agendaEventListener() {
        kSession.addEventListener(new DefaultAgendaEventListener() {
            public void afterMatchFired(AfterMatchFiredEvent event) {
                super.afterMatchFired(event);
                System.out.println(event);
            }
        });
    }

    /**
     * 打印所有工作空间中的事件
     */
    public void debugRuleRuntimeEventListener() {
        kSession.addEventListener(new DebugRuleRuntimeEventListener());
    }

    /**
     * 直接使用KieRuntimeLogger来做事
     */
    public void useKieRuntimeLogger() {
        KieRuntimeLogger logger =
                KieServices.Factory.get().getLoggers().newFileLogger(kSession, "logdir/mylogfile");
        logger.close();
    }
}
